// GameBoardDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Client.h"
#include "GameBoardDlg.h"


// CGameBoardDlg dialog

IMPLEMENT_DYNAMIC(CGameBoardDlg, CPropertyPage)

CGameBoardDlg::CGameBoardDlg()
: CPropertyPage(CGameBoardDlg::IDD)
{
	invitedUser=new char[20];
}

CGameBoardDlg::~CGameBoardDlg()
{
}

void CGameBoardDlg::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_USERLIST, _myUserBox);
	DDX_Control(pDX, IDC_YOURNAME, _myName);
	DDX_Control(pDX, IDC_YOURRIVALNAME, _myRivalName);
	DDX_Control(pDX, IDC_STATUS,_myStatus);
}


BEGIN_MESSAGE_MAP(CGameBoardDlg, CPropertyPage)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDC_INVITE, &CGameBoardDlg::OnBnClickedInvite)
	ON_LBN_SETFOCUS(IDC_USERLIST, &CGameBoardDlg::OnLbnSetfocusUserlist)
	ON_LBN_SELCHANGE(IDC_USERLIST, &CGameBoardDlg::OnLbnSelchangeUserlist)
	ON_EN_CHANGE(IDC_STATUS, &CGameBoardDlg::OnEnChangeEdit2)
	ON_BN_CLICKED(IDC_NEW, &CGameBoardDlg::OnBnClickedNew)
	ON_BN_CLICKED(IDC_QUIT, &CGameBoardDlg::OnBnClickedQuit)
	ON_BN_CLICKED(IDC_EXIT, &CGameBoardDlg::OnBnClickedExit)
	ON_BN_CLICKED(IDC_LOGOUT, &CGameBoardDlg::OnBnClickedLogout)
	ON_BN_CLICKED(IDC_EDIT_PROFILE, &CGameBoardDlg::OnBnClickedEditProfile)
END_MESSAGE_MAP()


// CGameBoardDlg message handlers


int CGameBoardDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CPropertyPage::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	_gameBoard= GameBoardController::getIntance();
	return 0;
}

void CGameBoardDlg::Update()
{

}
void CGameBoardDlg::Draw()
{

}
void CGameBoardDlg::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	_myName.SetWindowText(CClientApp::getUser()->UserName());
	_myRivalName.SetWindowText(CClientApp::getRivalUser()->UserName());
	CDC *cDC=this->GetDC();
	Graphics graphics(dc);
	CRect rec(_gameBoard->X(),_gameBoard->Y(),_gameBoard->Width(),_gameBoard->Height());
	dc.Rectangle(rec);
	int startX=_gameBoard->X();
	int startY=_gameBoard->Y();
	int i=1;
	while(startX<_gameBoard->Width() && startY < _gameBoard->Height()){
		startX=_gameBoard->X()+_gameBoard->Delta()*i;
		startY=_gameBoard->Y();
		dc.MoveTo(startX,startY);
		dc.LineTo(startX,_gameBoard->Height());

		startX=_gameBoard->X();
		startY=_gameBoard->Y()+_gameBoard->Delta()*i;
		dc.MoveTo(startX,startY);
		dc.LineTo(_gameBoard->Width(),startY);
		i++;
	}
	for (int i=0; i<_gameBoard->Size();i++)
	{
		for (int j=0; j<_gameBoard->Size();j++)
		{
			if (_gameBoard->Board()[i][j]!=0)
			{
				MyBitmap* ima;
				if (_gameBoard->Board()[i][j]==1)
				{
					ima=new MyBitmap(LPCTSTR(IDB_BITMAP1));
				}
				if (_gameBoard->Board()[i][j]==2)
				{
					ima=new MyBitmap(LPCTSTR(IDB_BITMAP2));
				}
				int x=j*_gameBoard->Delta()+_gameBoard->X()+1;
				int y=i*_gameBoard->Delta()+_gameBoard->Y()+1;

				CDC *memDC=new CDC();
				CBitmap *bmold=new CBitmap();
				memDC->CreateCompatibleDC(cDC);
				bmold->CreateCompatibleBitmap(cDC,19,19);
				memDC->SelectObject(bmold);

				ima->DrawTransparent(memDC,0,0,RGB(0,0,0));
				cDC->BitBlt(x,y,19,19,memDC,0,0,SRCCOPY);
			}
		}
	}
	int status=GameBoardController::getIntance()->Status();
	switch (status)
	{
	case 1:
		_myStatus.SetWindowText(L"Vui long ket noi voi nguoi choi khac de bat dau choi");
		break;
	case 2:
		_myStatus.SetWindowText(L"Vui long cho doi thu cua ban chap nhan ket noi");
		break;
	case 3:
		_myStatus.SetWindowText(L"Den luot di cua ban");
		break;
	case 4:
		_myStatus.SetWindowText(L"Den luot di cua doi thu");
		break;
	case 5:
		_myStatus.SetWindowText(L"Ket noi that bai, vui long chon doi thu khac de ket noi");
		break;
	case 6:
		_myStatus.SetWindowText(L"Khong the ket noi den server, vui long thu lai sau");
		break;
	}
	int n=GameBoardController::getIntance()->OnlineUser().size();
	_myUserBox.ResetContent();
	for (int i=0; i<n;i++)
	{
		USES_CONVERSION;
		LPCTSTR temp = A2W(GameBoardController::getIntance()->OnlineUser()[i]);
		_myUserBox.AddString(temp);																
	}

	int isLose=GameBoardController::getIntance()->IsLose();
	if (isLose)
	{
		MessageBox(L"Ban da thua roi",L"Loser",MB_SYSTEMMODAL);
		GameBoardController::getIntance()->Init();
		Invalidate();
	}

	// Do not call CPropertyPage::OnPaint() for painting messages
	
}

void CGameBoardDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	int result=_gameBoard->checkPoint(point.x,point.y);
	bool isWin=_gameBoard->IsWin();
	if (result!=-1)
	{
		Invalidate();
		CUser* us=CClientApp::getUser();
		CMessage iMes;
		char* iUser=new char[20];
		iUser=CHelper::ConverToChar(us->UserName(),iUser);
		char *rival=new char[20];
		rival=CHelper::ConverToChar(CClientApp::getRivalUser()->UserName(),rival);
		iMes.CreateCheckMessage(iUser,rival,result);
		MySocket::getIntance()->SendMessToServer(iMes);
		_gameBoard->Turn(false);
		GameBoardController::getIntance()->Status(4);
		if (isWin)
		{
			MessageBox(L"Chuc mung ban da thang",L"Winer",MB_SYSTEMMODAL);
			_gameBoard->Init();
			this->Invalidate();
		}
		delete iUser;
	}
	CPropertyPage::OnLButtonDown(nFlags, point);
}
void CGameBoardDlg::OnBnClickedInvite()
{
	// TODO: Add your control notification handler code here
	for (int i=0; i<oldInvitedUser.size();i++)
	{
		char* temp=new char[20];
		strcpy(temp,oldInvitedUser[i]);
		if (!strcmp(invitedUser,temp))
		{
			MessageBox(L"Doi thu nay da duoc ban moi",L"Thong bao",MB_OK);
			return;
		}
	}
	if (invitedUser!="")
	{
		switch(_gameBoard->Status()){
			case 0:
				break;
			case 3:
			case 4:
				MessageBox(L"Ban khong the moi nguoi khac trong khi dang choi",L"Thong bao",MB_OK);
				return;
		}
		char *rival=new char[20];
		char *user=new char[20];
		user=CHelper::ConverToChar(CClientApp::getUser()->UserName(),user);
		strcpy(rival,invitedUser);
		if (rival==CClientApp::getUser()->UserName())
		{
			MessageBox(L"Ban khong the chon doi thu la chinh ban",L"Thong bao",MB_OK);
			return;
		}
		CMessage mes;
		mes.CreateConnect(user,rival);
		MySocket::getIntance()->SendMessToServer(mes);
		GameBoardController::getIntance()->Status(2);
		Invalidate();
		char* temp=new char[20];
		strcpy(temp,invitedUser);
		oldInvitedUser.push_back(temp);
	}
	else
	{
		MessageBox(L"Ban vui long chon doi thu truoc moi co the ket noi",L"Thong bao",MB_OK);
	}
}

void CGameBoardDlg::OnLbnSetfocusUserlist()
{
	// TODO: Add your control notification handler code here

}

void CGameBoardDlg::OnLbnSelchangeUserlist()
{
	// TODO: Add your control notification handler code here
	int index;
	CString strText;
	index = _myUserBox.GetCurSel();
	if (index>=0)
	{
		_myUserBox.GetText(index,strText);
		invitedUser=CHelper::ConverToChar(strText,invitedUser);
	}
}

void CGameBoardDlg::OnEnChangeEdit2()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CPropertyPage::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.

	// TODO:  Add your control notification handler code here
}

void CGameBoardDlg::OnBnClickedNew()
{
	// TODO: Add your control notification handler code here
	if (GameBoardController::getIntance()->Status()==3)
	{
		_gameBoard->Init();
		this->Invalidate();
		char *rival=new char[20];
		CMessage mes;
		rival=CHelper::ConverToChar(CClientApp::getRivalUser()->UserName(),rival);
		mes.CreateNewGame(rival);
		MySocket::getIntance()->SendMessToServer(mes);
	}
	else{
		MessageBox(L"Ban chi co the tao game moi tai luot di cua ban",L"Thong bao",MB_OK);
	}
}

void CGameBoardDlg::OnBnClickedQuit()
{
	// TODO: Add your control notification handler code here
	char *rival=new char[20];
	CMessage mes;
	rival=CHelper::ConverToChar(CClientApp::getRivalUser()->UserName(),rival);
	mes.CreateQuitGame(rival);
	MySocket::getIntance()->SendMessToServer(mes);
	GameBoardController::getIntance()->QuitGame();
	this->Invalidate();
	oldInvitedUser.resize(0);
}


void CGameBoardDlg::OnBnClickedExit()
{
	// TODO: Add your control notification handler code here
	GameBoardController::getIntance()->IsExit(true);
}


void CGameBoardDlg::OnBnClickedLogout()
{
	// TODO: Add your control notification handler code here
	Logout();

}

void CGameBoardDlg::OnBnClickedEditProfile()
{
	// TODO: Add your control notification handler code here
	if(_gameBoard->Status()==1){
		char *user=new char[20];
		user=CHelper::ConverToChar(CClientApp::getUser()->UserName(),user);
		CMessage mes;
		mes.CreateLogOut(user);
		MySocket::getIntance()->SendMessToServer(mes);
		MySocket::getIntance()->IsChange(true);
		MySocket::getIntance()->ActivePage(2);
	}
	else{
		MessageBox(L"Ban khong the chinh sua thong tin trong khi dang choi",L"Thong bao",MB_OK);
	}
}


void CGameBoardDlg::Logout()
{
	char *user=new char[20];
	user=CHelper::ConverToChar(CClientApp::getUser()->UserName(),user);
	CMessage mes;
	mes.CreateLogOut(user);
	MySocket::getIntance()->SendMessToServer(mes);
	MySocket::getIntance()->IsChange(true);
	MySocket::getIntance()->ActivePage(0);
	oldInvitedUser.resize(0);
}

void CGameBoardDlg::Init()
{
	oldInvitedUser.resize(0);
	Invalidate();
}
